<template>
	<view>
		<view>
			<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
			<u-form labelPosition="left" ref="myform">

				<view style="padding: 7px;" v-show="true" class="card-container">
					<view style="padding: 7px;">附件</view>
					<u-cell-group>
						<u-cell v-for="(item) in items_fj">
							<view slot="title" class="u-slot-title">
								<u-link :href="item.url" :text="item.fj_cnname" color="#19be6b" line-color="#19be6b">
								</u-link>

							</view>
						</u-cell>
					</u-cell-group>


				</view>


				<view style="padding: 7px;" class="card-container">

					<view style="padding: 7px;">业务流程</view>
					<u-steps ref="elstep" :current="act" direction="column">
						<u-steps-item :title="item.title" :desc="item.description" v-for="item in stepdata"
							:key="index">
						</u-steps-item>

					</u-steps>

				</view>
				<view style="padding: 7px;" class="card-container">
					<view style="padding: 7px;overflow: hidden">意见清单</view>
					<vol-table
					      url="api/Fk_reimfloat/getPageData"  
					      ref="float_table"
						  :height="float_height"
					      direction="horizontal"
						  :defaultLoadPage="lood"
						  @loadBefore="float_loadBefore"
					      :columns.sync="float_columns"
						  @loadAfter="float_loadAfter"
					    >
					    </vol-table>
				</view>
				<view style="padding: 7px;" class="card-container">
				
					<vol-form :load-key="true" ref="form" :form-options.sync="editFormOptions"
						:formFields.sync="editFormFields">
					</vol-form>
				</view>
				<view style="padding: 7px;" class="card-container">
					<u-row justify="end">
						<u-col span="3">
							<u-button type="primary" shape="circle" @click="up_tijiao()" text="提交"></u-button>
						</u-col>
					</u-row>
				</view>
			</u-form>
		</view>
	</view>
</template>

<script>
	import h_common from '@/util/h_common.js';
	export default {
		data() {
			return {
				//ls_alldata:null,
				lood:false,
				float_height:300,
				items_fj: [{}], //附件
				isshow: false,
				index: null,
				ls_active: 0,
				toData: [],
				act: 0,
				stepdata: [],
				curr_row: null,
				float_columns:[
					                       {field:'stepid',title:'步骤',type:'int',bind:{ key:'steplist',data:[]},width:100,readonly:true,align:'left'},
					                       {field:'username',title:'姓名',type:'string',bind:{ key:'uerlist',data:[]},width:100,readonly:true,align:'left'},
					                       {field:'bdatetime',title:'时间',type:'datetime',width:130,readonly:true,align:'left',sort:false},
					                       {field:'reyijian',title:'意见',type:'string',width:120,align:'left'},
					                       {field:'remark',title:'意见详情',type:'string',width:220,readonly:true,align:'left'},	
				],
				editFormFields: {
					reyijian: '同意',
					remark: ""
				},
				editFormOptions: [{
						title: '提交意见',
						type: 'select',
						required: true,
						field: 'reyijian',
						data: [{
								key: "同意",
								value: "同意"
							},
							{
								key: "不同意",
								value: "不同意"
							},
						],
					},
					{
						title: '审核意见',
						field: 'remark',
						type: 'textarea'
					},
				],
			};
		},
		methods: {
			float_loadAfter(data, callback) {
			      //api数据加载后的方法
				  //console.log(data)
				  data.rows=data.rows.sort(function(a, b){return a.fk_reimfloat_id - b.fk_reimfloat_id});//根据fk_reimfloat_id字段进行排序
				  //console.log(data.rows[0].bdatetime)
				  //console.log(data.rows[1].bdatetime)
			      callback(true);
			    },
			float_loadBefore(params, callback) {
			      //数据加载前方法,可以自己设置查询条件
			      // params.wheres.push({ name: '字段', value: '值' });
			     
			      // params.value='设置一些其他值'
				   //console.log(this.curr_row.reim_id)
				  params.wheres.push({ name: 'reimid', value: this.curr_row.reim_id });
				 
			      callback(true);
		    },
			async up_tijiao() {
				var _alldata; // = uni.getStorageSync("_alldata");	
				//VOL平台的同步执行
				await this.http.post("/api/Fk_reim/getPageData", {}, true).then((result) => {
					_alldata = result;
				})
				//console.log(_alldata)
				//console.log(this.ls_alldata)
				//return;
				var reim_id = this.curr_row.reim_id;
				var stepid = this.curr_row.stepid;
				let _reimlist = _alldata.extra.reimlist;
				let _rf_sqj = _alldata.extra.rf_sqj; //财务是否审批解除预算限制
				let _ys_query = _alldata.extra.ys_query; //预算
				let cys = 0;
				//console.log(_rf_sqj);
				for (let ii = 0; ii <= _reimlist.length - 1; ii++) { //只有for才能break
					let row = _reimlist[ii];
					if (row.reim_id == reim_id) {
						//console.log(row);
						let _tt = _ys_query.filter(x => {
							//console.log(x.kyed);	
							//console.log(row.je);	
							return x.racct == row.fei_type && x.rcntr == row.rcntr && row.je <= x.kyed && row
								.je >
								0; //满足这些条件才允许提交,预算允许范围
							//return x.racct == "6602020101" && x.rcntr == "9000050001" 
						});
						//console.log(_tt);
						if (_tt.length > 0) { //满足提交
							cys = 0;
						} else {
							cys = 1; //超预算或者预算为空没填
							break; //当有多条行项目，其中有一条不满足就break
						}
					}
				}
				/* _reimlist.forEach(row => {
				
				}); */

				if (_rf_sqj.some(s => s.reimid == reim_id && s.node_type == "sqj_ok") == true) {
					cys = 0; //直接通过
				}
				if (cys == 1) {
					this.$toast("不允许提交！请检查科目、成本中心、是否超预算。");
					return;
				}
				if (this.tickno_rows > "1") { //>1表示票号有重复,同样的票号有一个以上
					this.$toast("票号重复或空，不允许提交，请检查！");
					return;
				}
				//console.log(this.$store.getters.data().keyed);
				//return;

				/* if (stepid == 9) { //原来是9999
					return this.$toast("当前流程已经结算，不能提交！")
				} */

				//console.log(this.$refs.reimfloat_form1.$refs.myform.formFields)
				let ls_type = "pre";
				let yj = this.editFormFields.reyijian;
				if (yj == "同意") {
					ls_type = "pre";
				} else {
					ls_type = "back"; //驳回
				}
				var postdata = this.editFormFields;
				this.toData.splice(0,this.toData.length);//pdata汇签人，移动端暂时没有汇签人
				var pdata =encodeURIComponent(JSON.stringify(this.toData)); //
				//console.log(JSON.stringify(this.toData));
				var turl = "/api/Fk_reim/up_tijiao?reimid=" + reim_id + "&current_stepid=" + stepid + "&tp=" +
					ls_type +
					"&pdata=" + pdata;
				this.http.post(turl, postdata, true).then((result) => {
					//console.log(result);
					//  this.$parent.$parent.$parent.$parent.$parent.$parent.$refs.reimfloat1.$refs.grid.refresh();//找到上一层的兄弟控件刷新，查询新数据
					if (!result.status) {
						return this.$toast(result.message)
					} else {
						uni.switchTab({
							url: "/pages/home/home"
						});
						/* if (this.curr_row.typeid == 1) {
							let url = "/pages/mytable/Fk_reim/Fk_reim";
							uni.navigateTo({
								url: url
							});
						} else {
							uni.navigateTo({
								url: "/pages/mytable/Fk_reim/Fk_reim_cc"
							})
						} */

						/* this.model.box3 = false;
						this.$emit("parentCall", ($vue) => { //大哥你这写的什么代码，把刷新放到异步请求的外面，我也是服了2021年12月2日 22:32:46
							$vue.search(); //search()为ViewGrid控件的search()
						}); //刷新父控件查询 */
						//this.$toast(result.message);
					}
				});


			},
			dfile(_lsurl) {
				console.log(_lsurl);
				let url = this.http.ipAddress + "api/customer_jr_mobile/down_bytes_file";
				uni.showLoading({
					mask: true,
					title: "下载中..."
				});

				/* uni.request({
					url: url, //请求文件流地址
					method: 'POST',
					data: [], //请求参数
					responseType: "ArrayBuffer", //Object/String/ArrayBuffer
					success: res => {
						let datas = res.data
						//console.log(res)
						this.qrUrl = 'data:image/png;base64,' + h_common.base64_encode(new Uint8Array(datas)
							.reduce((datas,
								byte) => datas + String.fromCharCode(byte), ''));
						this.qrUrl = this.qrUrl.replace(/[\r\n]/g, "");
					},
					fail: () => {},
					complete: () => {},
				}); */

				uni.hideLoading();
			},


			get_reimfloat_tu(reim_id) { //给流程图加载数据
				this.$nextTick(() => {
					var turl = "/api/Fk_reim/get_reimfloat_data?reim_id=" + reim_id;
					this.http.post(turl, {}, true).then((result) => {
						//console.log(result.fk_reimfloat);
						this.stepdata.splice(0, this.stepdata.length);
						this.act = 0;
						//var obj;此处为坑，var定义要放在for巡皇里面，否则前台界面更新不完整huangxs2021年12月10日 21:43:17
						for (let ii = 0; ii <= result.fk_reimfloat.length - 1; ii++) {
							//console.log(result.fk_reimfloat[ii])
							//console.log(this.curr_row.stepid)
							if (result.fk_reimfloat[ii].stepid == this.curr_row.stepid) {
								//this.act=i;//表示当前步骤号
								this.ls_active = ii;
								//console.log(this.ls_active+"dd");
							};
							let realname = "";
							if (result.fk_reimfloat[ii].usertruename != null) {
								realname = "," + result.fk_reimfloat[ii].usertruename
							}
							let _bdatetime = "";
							if (result.fk_reimfloat[ii].bdatetime != null) {
								_bdatetime = result.fk_reimfloat[ii].bdatetime + ","
							}
							let obj = {
								title: result.fk_reimfloat[ii].stepname,// + realname,
								description:'',
								/* description: _bdatetime + result
									.fk_reimfloat[ii].remark.replace(/<.*?>/g, ""), */
								//index:result.fk_reimfloat[i].stepid,
							}
							this.stepdata.push(obj);
						}
						this.act = this.ls_active;
						//加载提交人员数据
						this.toData.splice(0, this.toData.length);
						for (let ri = 0; ri <= result.next_ren.length - 1; ri++) {
							this.toData.push({
								username: result.next_ren[ri].userName,
								usertruename: result.next_ren[ri].userTrueName,
							})

						}
					});


				});
			},

		},
		onUnload() { //关闭界面的触发事件
			//如果多端发布的话判断一下当前操作的客户端 
			let url = "/pages/mytable/Fk_reim/Fk_reim";
			//#ifdef MP-WEIXIN
			wx.reLaunch({
				url: url
			})
			//#endif
		},
		onBackPress(event) { //点返回按钮回到首页
			let url = "/pages/mytable/Fk_reim/Fk_reim";
			uni.navigateTo({
				url: url
			})
			return true; //返回true表示不执行默认的返回
		},
		onShow() {
			this.$nextTick(()=>{
				let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let params = routes[routes.length - 1].options; //获取路由参数 
				this.curr_row = uni.getStorageSync("curr_row");
				//console.log(this.curr_row);
				//return 
				if (this.curr_row.fujian != null) {
					let fujian_str = this.curr_row.fujian.split(",");
					//console.log(fujian_str);
					this.items_fj.splice(0, this.items_fj.length);
					for (let i = 0; i <= fujian_str.length - 1; i++) {
						let t = fujian_str[i].split("/");
						this.items_fj.push({
							fj_cnname: t[t.length - 1],
							url: this.http.ipAddress + fujian_str[i],
						})
					}
				}
				//this.$refs.float_table.load(null,true)//
				
				
				var ls_remark = "";
				this.get_reimfloat_tu(params.reimid);
				this.$refs.float_table.load(null,true)//
			})
			
		},

	};
</script>
<style scoped lang="less">
	.card-container {
		box-shadow: 1px 1px 9px #efefef;
		margin: 20rpx;
		border: 1px solid #f1f1f1;
		border-radius: 10rpx;
		padding: 20rpx 4rpx;

		.fn-title {
			font-size: 28rpx;
			color: #8f9ca2;
			padding: 4rpx 0 30rpx 20rpx;
		}

		.grid-text {
			padding-top: 8rpx;
			font-size: 26rpx;
		}

	}
</style>
